'use strict';

const Controller = require('egg').Controller;

class SearchApi extends Controller {

  async searchApiBaidu(ctx) {
    const token = ctx.val('token');
    const kw = ctx.val('kw');
    const now_page = parseInt(ctx.val('page')) || 1;

    ctx.err(token != this.config.search_proxy.token, '非法搜索请求, 要注意');
    const topics = await ctx.service.searchSvc.fetchBaidu(kw, now_page);
    return ctx.ok(topics);
  }

  async searchApiTsou(ctx) {
    const token = ctx.val('token');
    const kw = ctx.val('kw');
    const now_page = parseInt(ctx.val('page')) || 1;

    ctx.err(token != this.config.search_proxy.token, '非法搜索请求, 要注意');
    const topics = await ctx.service.searchSvc.fetchGoogle(kw, now_page);
    return ctx.ok(topics);
  }


  async searchApiZhihu(ctx) {
    const token = ctx.val('token');
    const kw = ctx.val('kw');

    ctx.err(token != this.config.search_proxy.token, '非法搜索请求, 要注意');
    const topics = await ctx.service.searchSvc.fetchZhihu(kw);
    return ctx.ok(topics);
  }

}

module.exports = SearchApi;
